#include "sqlite-sql-command.h"


sql_command_base::sql_command_base( std::string &str_name )
    : m_str_table_name( str_name )
{

}
sql_command_base::~sql_command_base()
{

}

void sql_command_base::set_field_item_value( const std::string str_field, const int i_value )
{
    set_field_item_value( str_field, i_value, m_key_value_list );
}

void sql_command_base::set_field_item_value( const std::string str_field, const unsigned int ui_value )
{
    set_field_item_value( str_field, ui_value, m_key_value_list );
}

void sql_command_base::set_field_item_value( const std::string str_field, const signed long long val )
{
    set_field_item_value( str_field, val, m_key_value_list );
}

void sql_command_base::set_field_item_value( const std::string str_field, const char c_char)
{
    set_field_item_value( str_field, c_char, m_key_value_list );
}

void sql_command_base::set_field_item_value( const std::string str_field, const std::string &str_val)
{
    if( std::string::npos != str_val.find( "'" ) ){
        std::string str_temp = str_val;
        std::string old_value = "'";
        std::string new_value = "\"";
        while(true)   {
          std::string::size_type   pos(0);
          if( ( pos = str_temp.find( "'" ) ) != std::string::npos ){
            str_temp.replace( pos, old_value.length(), new_value );
          } else {
              break;
          }
       }
       set_field_item_value( str_field, str_temp, m_key_value_list );
       return;
    }

    set_field_item_value( str_field, str_val, m_key_value_list );
}

void sql_command_base::set_field_item_value( const std::string str_field, const int val, std::list<field_item_value>& key_value)
{
    std::string str_value = convert_to_string( val );
    set_field_item_value( str_field, str_value, key_value );
}

void sql_command_base::set_field_item_value( const std::string str_field, const unsigned int val, std::list<field_item_value>& key_value)
{
    std::string str_value = convert_to_string( val );
    set_field_item_value( str_field, str_value, key_value );
}

void sql_command_base::set_field_item_value( const std::string str_field, const signed long long val, std::list<field_item_value>& key_value)
{
    std::string str_value = convert_to_string( val );
    set_field_item_value( str_field, str_value, key_value );
}

void sql_command_base::set_field_item_value( const std::string str_field, const char val, std::list<field_item_value>& key_value)
{
    std::string str_value = convert_to_string( val );
    set_field_item_value( str_field, str_value, key_value );
}

void sql_command_base::set_field_item_value( const std::string str_field, const std::string& val, std::list<field_item_value>& key_value)
{
    field_item_value item;
    item.str_field = str_field;
    item.str_value = val;
    key_value.push_back( item );
}


sql_insert_command::sql_insert_command( std::string &str_name )
    : sql_command_base( str_name )
{

}

sql_insert_command::~sql_insert_command()
{

}

std::string sql_insert_command::create_sql_statement()
{
    if( m_str_table_name.empty() ){
        assert( 0 );
    }

    std::string str_sql = "";
    if( m_key_value_list.empty() ){
        return str_sql;
    }

    str_sql = "insert into ";
    str_sql += m_str_table_name;
    str_sql += "(";

    std::list<field_item_value>::iterator it = m_key_value_list.begin();
    for( ; it != m_key_value_list.end(); ){
        str_sql += "'";
        str_sql += it->str_field;
        str_sql += "'";

        ++it;
        if( it == m_key_value_list.end() ){
            str_sql += ")";
        } else {
            str_sql += ", ";
        }
    }

    str_sql += "values(";

    it = m_key_value_list.begin();
    for( ; it != m_key_value_list.end(); ){
        str_sql += "'";
        str_sql += it->str_value;
        str_sql += "'";

        ++it;
        if( it == m_key_value_list.end() ){
            str_sql += ")";
        } else {
            str_sql += ", ";
        }
    }

    return str_sql;
}

sql_update_command::sql_update_command( std::string &str_name )
    : sql_command_base( str_name )
{

}

sql_update_command::~sql_update_command()
{

}

void sql_update_command::fill_item_where( const std::string &str_field, const int i_val )
{
    set_field_item_value( str_field, i_val, m_where_key_val );
}

void sql_update_command::fill_item_where( const std::string &str_field, const std::string &str_val )
{
    set_field_item_value( str_field, str_val, m_where_key_val );
}

std::string sql_update_command::create_sql_statement()
{
    if( m_str_table_name.empty() ){
        assert( 0 );
    }

    std::string str_sql = "";
    if( m_key_value_list.empty() ){
        assert( 0 );
        return str_sql;
    }

    str_sql = "update ";
    str_sql += m_str_table_name;
    str_sql += " set ";

    std::list<field_item_value>::iterator it = m_key_value_list.begin();
    for( ; it != m_key_value_list.end(); ){
        str_sql += "[";
        str_sql += it->str_field;
        str_sql += "]='";
        str_sql += it->str_value;
        str_sql += "'";
        ++it;
        if (m_key_value_list.end() != it) {
            str_sql += ",";
        }
    }

    if( !m_where_key_val.empty() ){
        str_sql += " where ";

        std::list<field_item_value>::iterator it_where = m_where_key_val.begin();
        for( ; it_where != m_where_key_val.end();  ){
            str_sql += "[";
            str_sql += it_where->str_field;
            str_sql += "]";
            str_sql += "='";
            str_sql += it_where->str_value;
            str_sql += "'";
            ++it_where;
            if (m_where_key_val.end() != it_where) {
                str_sql += " and ";
            }
        }
    }

    return str_sql;
}

sql_delete_command::sql_delete_command( std::string &str_name )
    : sql_update_command( str_name )
{

}

sql_delete_command::~sql_delete_command()
{

}

std::string sql_delete_command::create_sql_statement()
{
    if ( m_str_table_name.empty() ) {
        assert(0);
    }

    std::string str_sql = "";
    if ( m_where_key_val.empty() ) {
        str_sql = "delete from " + m_str_table_name;
        return str_sql;
    }

    str_sql = "delete from " + m_str_table_name + " where ";

    std::list<field_item_value>::iterator it_where = m_where_key_val.begin();
    for ( ; it_where != m_where_key_val.end(); ) {
        str_sql += "[";
        str_sql += it_where->str_field;
        str_sql += "]";
        str_sql += "='";
        str_sql += it_where->str_value;
        str_sql += "'";
        ++it_where;
        if (m_where_key_val.end() != it_where) {
            str_sql += " and ";
        }
    }

    return str_sql;
}

std::string sql_delete_command::create_delete_all_sql_statement()
{
    std::string str_sql = "delete from " + m_str_table_name;
    return str_sql;
}


sql_create_table_command::sql_create_table_command( const std::string &str_name )
    : mstr_table_name( str_name )
{

}

sql_create_table_command::~sql_create_table_command()
{

}

void sql_create_table_command::fill_item_field( const sql_column_info &col_info )
{
    m_field_list.push_back( col_info );
}

std::string sql_create_table_command::create_sql_statement()
{
    std::string str_sql = "";
    if( m_field_list.empty() ){
        assert( 0 );
        return str_sql;
    }

    str_sql = "CREATE TABLE [" + mstr_table_name + "] (";

    std::list<sql_column_info>::iterator it = m_field_list.begin();
    for( ; it != m_field_list.end(); ){
        str_sql += "[";
        str_sql += it->mstr_name;
        str_sql += "]";

        str_sql += it->get_field_type_str();

        ++it;

        if( it == m_field_list.end() ){
            str_sql += ")";
        } else {
            str_sql += ",";
        }
    }

    return str_sql;
}

sql_update_table_command::sql_update_table_command( const std::string &str_name )
    : mstr_table_name( str_name )
{

}

sql_update_table_command::~sql_update_table_command()
{

}

void sql_update_table_command::fill_item_field( const sql_column_info &col_info )
{
    m_field = col_info;
}

std::string sql_update_table_command::create_sql_statement()
{
    std::string str_sql = "ALTER TABLE [" + mstr_table_name + "] ADD [" + m_field.mstr_name + "] " + m_field.get_add_field_type_str();
    return str_sql;
}
